<?php

namespace App\Http\Controllers\Admin;

use App\Models\Permission;
use App\Models\Role;
use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class IndexController extends Controller
{
	/**
	 * 后台首页
	 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
	 */
	public function index() {
	    if (auth()->user()->username == 'admin'){
	        $permissions = Permission::where('is_menu','1')->get()->toArray();
	        $menu = subTree($permissions);
        }else {
            $menu = [];
            $info = User::where('id', auth()->id())
                ->with('role')->first()->role;
            if (!is_null($info)) {
                $role_id = $info->id;
                $permissionsObj = Role::with('permissions')->find($role_id)->permissions();
                $permissions = $permissionsObj->where('is_menu', '1')->get()->toArray();
                $menu = subTree($permissions);
            }
        }
		return view('admin.index.index',compact('menu'));
	}

	/**
	 * 后台欢迎页
	 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
	 */
	public function welcome() {
		return view('admin.index.welcome');
	}
}